<?php


namespace app\admin\model;



use think\Db;
use think\Model;

class CemeteryModel extends BaseModel
{

    protected $name = 'cemetery';

    protected $field="id,name,parent_id,parent_id as pId,type,is_generate,status,list_order";
    protected $temp=[];

    public function getList(){
        $info_child = $this->getChild(0);

        cache('cemetery_data',$info_child);
        return $info_child;
    }

    protected function getChild($pid = 0, $t = 0,$srt='') {
        $t++;
        $data = $this->where(['parent_id'=>$pid,'is_delete'=>0])->field($this->field)->order('list_order ASC')->select()->toArray();
        if (!empty($data)) {
            foreach ($data as $key => $val) {
                $val['nav']  = $val['name'].'-'.$srt;
                $this->temp[] = $val;
                $this->getChild($val['id'], $t,$val['name']);
            }
        }
        return $this->temp;
    }

    public function cacheCemeteryData($is_cache=0){
        if ($is_cache!=0){
            $info_child = $this->getChild(0);
            cache('cemetery_data',$info_child);
        }else{
            $cemetery_data = cache('cemetery_data');
            if (!$cemetery_data){
                $cemetery_data = $this->getChild(0);
            }
           return $cemetery_data;
        }
    }
    public function getListTreeWhere(){
        $info_child = $this->getChildwhere(0);
        return $info_child;
    }
    protected function getChildwhere($pid = 0, $t = 0) {
        $t++;
        $data = $this
            ->field("c.id,c.name,c.parent_id as pId,c.type")
            ->join("dj_tombarea t","c.id = t.cemetery_id","left")
            ->join("dj_tombarea_row r","t.id = r.tombarea_id","left")
            ->join("dj_tombarea_row_col rc","r.rowID = rc.rowid","left")
            ->where(['parent_id'=>$pid,'is_delete'=>0])->field($this->field)->order('list_order ASC')->select()->toArray();
        if (!empty($data)) {
            foreach ($data as $key => $val) {
                $this->temp[] = $val;
                $this->getChild($val['id'], $t);
            }
        }
        return $this->temp;
    }

    /**
     *  获取每级名称
     * @param $pid
     * @param array $array
     * @param string $info_arr
     * @return mixed|string
     */
    public function  getNavCates($pid ,$array=[],$info_arr=''){
        static $level = 1;
        $is_parent = Db::name( 'cemetery')->where(["id"=>$pid])->find();
        if (!empty($info_arr)){
            $info_arr = $is_parent['name'].'->'.$info_arr;
        }else{
            $info_arr = $is_parent['name'];
        }
        $array[] = $is_parent;
        if ( $is_parent["parent_id"] ) {
            $level++;
            return $this->getNavCates( $is_parent['parent_id'],$array,$info_arr);
        }
        return $info_arr;
    }


    public static function getSup($id)
    {
        // 拼接查询条件hql
        $res = self::where(array('is_delete' => 0, 'id' => $id))->select()->toArray();
        // 得到一级数据
        if ($res) {
            if ($res[0]["parent_id"] != 0) {
                $result = self::getSup($res[0]["parent_id"]);
            } else {
                $result = array();
            }
            $imgInfo = array_merge_recursive($res, $result);
        } else {
            $imgInfo = array();
        }
        return $imgInfo;
    }




}